package org.example.dom4j;

import jxl.Cell;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.dom.DOMElement;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StringXmlOutput {

    public static void main(String[] args) {
        try {
            outputAll("C:\\Users\\hp\\Desktop\\values");
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (WriteException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * dom4j
     * 输出所有的kv
     * @param valueFolder
     * @throws DocumentException
     * @throws IOException
     * @throws WriteException
     */
    private static void outputAll(String valueFolder) throws DocumentException, IOException, WriteException {
        File file = new File(valueFolder);

        //拿到所有语言类型的文件 类型按照文件名称声明的来
        File[] allValues = file.listFiles();
        HashMap<String, File> allFileMap = new HashMap<>();
        for (File allValue : allValues) {
            String[] split = allValue.getName().split("-", 2);
            if (split.length < 2) {
                allFileMap.put("en", new File(allValue.getAbsoluteFile() + "/strings.xml"));
            } else {
                allFileMap.put(split[1], new File(allValue.getAbsoluteFile() + "/strings.xml"));
            }
        }

        //所有包含的key值
        List<String> arrayKeys = new ArrayList<>();
        //所有文件的k-v值
        List<HashMap<String, String>> allStringArray = new ArrayList<>();
        //获取所有文件的kv值
        Iterator<String> iterator = allFileMap.keySet().iterator();
        while (iterator.hasNext()) {
            String type = iterator.next();
            File stringXml = allFileMap.get(type);
            Document read = new SAXReader().read(stringXml);
            Element rootElement = read.getRootElement();
            DOMElement lang = new DOMElement("string");
            lang.setAttribute("name","lang");
            lang.setText(type);
            rootElement.add(lang);

            HashMap<String, String> stringArrayKv = new HashMap<>();
            List<Element> elementString = rootElement.elements("string");
            List<Element> stringArrayElement = rootElement.elements("string-array");
            for (Iterator it = elementString.listIterator(); it.hasNext(); ) {
                Element element = (Element) it.next();
                try{
                    String translatable = element.attribute("translatable").getValue();
                    if (translatable != null && translatable.equals("false")) continue;
                }catch (NullPointerException e){

                }
                String key = element.attribute("name").getValue();
                if (!arrayKeys.contains(key)){
                    arrayKeys.add(key);
                }
                if (element.elements().isEmpty()){
                    stringArrayKv.put(key,element.getText());
                }else {
                    String xml = element.asXML();
                    stringArrayKv.put(key,xml.substring(xml.indexOf(">")+1,xml.lastIndexOf("<")));
                }
            }
            for (int i = 0; i < stringArrayElement.size(); i++) {
                List<Element> items = stringArrayElement.get(i).elements("item");
                for (int j = 0; j < items.size(); j++) {
                    String key = items.get(j).getParent().attribute("name").getValue() + j;
                    if (!arrayKeys.contains(key)){
                        arrayKeys.add(key);
                    }
                    stringArrayKv.put(key, items.get(j).getText());
                }
            }
            allStringArray.add(stringArrayKv);
        }

        //创建xls，写入第一列的k
        WritableWorkbook workbook = Workbook.createWorkbook(new File("output.xls"));
        WritableSheet page = workbook.createSheet("page", 0);
        for (int i = 0; i < arrayKeys.size(); i++) {
            String key = arrayKeys.get(i);
            Label label = new Label(0,i,key);
            page.addCell(label);
        }

        //写入其他列的v
        for (int i = 0; i < arrayKeys.size(); i++) {
            String key = arrayKeys.get(i);
            for (int j = 0; j < allStringArray.size(); j++) {
                HashMap<String, String> kv = allStringArray.get(j);
                Label label = new Label(j + 1, i, kv.getOrDefault(key, ""));
                page.addCell(label);
            }
        }

        workbook.write();
        workbook.close();
    }
}
